<?php
function displayCalendar($year, $month)
{
    // 获取指定年月的第一天
    $firstDay = strtotime("$year-$month-01");

    // 获取该月的总天数
    $totalDays = date('t', $firstDay);

    // 获取该月的第一天是星期几
    $firstDayOfWeek = date('N', $firstDay);

    // 显示日历标题
    echo "<h2>" . date('Y年n月', $firstDay) . "</h2>";

    // 显示星期几标题
    echo "<table>";
    echo "<tr><th>一</th><th>二</th><th>三</th><th>四</th><th>五</th><th>六</th><th>日</th></tr>";

    // 显示日期
    echo "<tr>";

    // 在第一天前面填充空白格子
    for ($i = 1; $i < $firstDayOfWeek; $i++) {
        echo "<td></td>";
    }

    // 显示日期
    $currentDayOfWeek = $firstDayOfWeek;
    for ($day = 1; $day <= $totalDays; $day++) {
        // 如果是周日，换行
        if ($currentDayOfWeek == 8) {
            echo "</tr><tr>";
            $currentDayOfWeek = 1;
        }

        // 显示日期和星期
        echo "<td>$day</td>";

        $currentDayOfWeek++;
    }

    // 在最后一天后面填充空白格子
    if ($currentDayOfWeek != 8) {
        for ($i = $currentDayOfWeek; $i <= 7; $i++) {
            echo "<td></td>";
        }
    }

    echo "</tr>";
    echo "</table>";
}

// 显示2023年1月的日历
for ($i = 1; $i <= 12; $i++) {
    displayCalendar(2023, $i);
}
